// 树组件点击二级菜单的一次性变量
var once = true;
// 树组件弹出层index
var index0pen;




layui.use(["tree", "jquery"], function () {
    var tree = layui.tree;

    var $ = layui.jquery;
    //side-menu

    var sideMenuData = [];

    $.ajax({
        url: "/fileType",
        type: "GET",
        success: function (rep) {
            // console.log(rep);
            if (rep.status === 200) {
                sideMenuData = rep.data;

                let transformData = [];
                for (let index = 0; index < sideMenuData.length; index++) {
                    transformData[index] = !transformData[index]
                        ? {}
                        : transformData[index];
                    const el = transformData[index];
                    Object.assign(el, sideMenuData[index]);
                    el.title = sideMenuData[index].name;
                    el.field = sideMenuData[index].code;


                    //树组件id不能重复
                    el.OneId = el.id;
                    el.id = sideMenuData[index].code;
                    el.lazy = true;
                    $.ajax({
                        url: "/dire/code/" + el.field,
                        type: "GET",
                        success: function (rep) {
                            // console.log(rep);
                            if (rep.status == 200) {
                                if (!rep.data.length) {
                                    el.lazy = false;
                                    return;
                                }
                                if (rep.data[0].whet || rep.data[0].whet == true) {
                                    el.lazy = true;
                                }
                            }
                        },
                    });
                }

                // console.log(transformData);

                var inst1 = tree.render({
                    elem: "#side-menu",
                    id: "side-menu", //定义索引
                    showCheckbox: false, //是否显示复选框
                    showLine: false, //是否开启连接线。默认 true，若设为 false，则节点左侧出现三角图标。
                    accordion: true, //是否开启手风琴模式，默认 false
                    onlyIconControl: false, //是否仅允许节点左侧图标控制展开收缩。默认 false
                    isJump: false, //是否允许点击节点时弹出新窗口跳转。默认 false
                    edit: ["add", "update", "del"],
                    customOperate: true,
                    data: transformData,
                    text: {
                        defaultNodeName: "无数据",
                        none: "",
                    },



                    click: function (obj) {
                        // console.log(obj.data); //得到当前点击的节点数据
                        // console.log(obj.elem); //得到当前节点元素



                        // 由于lazy 与 whet 同值，而 whet 代表 目录（true）/文件（false）
                        // 只有文件才可以点击跳转
                        if (obj.data.OneId) {
                            sessionStorage.setItem("__fileType",obj.data.type);
                        }
                        if (!obj.data.lazy && !obj.data.OneId) {
                            var inner_iframe = document.getElementById("inner_iframe");
                            // console.log(obj.data.id);
                            inner_iframe.src = "/biaoge?id=" + obj.data.id;
                        }
                    },

                    spread: function (obj) {
                        if (obj.state == "open") {
                            setTimeout(() => {
                                var temp = [];
                                // 一级菜单与其子菜单请求的路径并不相同
                                let mid_url =
                                    obj.data.code == obj.data.id
                                        ? "/dire/code/"
                                        : "/dire/direId/";
                                $.ajax({
                                    url: mid_url + obj.data.id,
                                    type: "GET",
                                    async: false,
                                    success: function (rep) {
                                        // console.log(rep);
                                        var data = rep.data;
                                        let arr_temp = [];
                                        for (let index = 0; index < data.length; index++) {
                                            arr_temp[index] = !arr_temp[index] ? {} : arr_temp[index];
                                            const el = arr_temp[index];
                                            Object.assign(el, data[index]);
                                            el.title = data[index].name;
                                            el.field = data[index].code;
                                            el.lazy = el.whet;
                                        }
                                        // console.log(arr_temp);
                                        temp = arr_temp;
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    },
                                });

                                tree.lazytree(inst1.config.id, obj.elem, temp);
                            }, 0);
                            if (once) {
                                tree.reload("side-menu");
                                once = !once;
                            }
                        }
                    },
                    operate: function (obj) {
                        var type = obj.type; //得到操作类型：add、edit、del
                        var data = obj.data; //得到当前节点的数据
                        var elem = obj.elem; //得到当前节点元素
                        // console.log(data)
                        if (type === "add") {
                            //增加节点
                            let $name = $("#add-form input[name='name']");
                            let $sort = $("#add-form input[name='sort']");
                            let $code = $("#add-form input[name='code']");
                            let $direId = $("#add-form input[name='direId']");

                            $name.val("");
                            $sort.val("");
                            if (data.id == data.code) {
                                $code.val(data.code);

                                $direId.val("");
                            } else {
                                $direId.val(data.id);

                                $code.val("");
                                1;
                            }
                            index0pen = layer.open({
                                type: 1,
                                title: "新增子目录",
                                shadeClose: true,
                                area: ["350px", "300px"],
                                content: $("#add-node"),
                            });
                        } else if (type === "update") {
                            //修改节点
                            let $name = $("#update-form input[name='name']");
                            let $sort = $("#update-form input[name='sort']");
                            let $code = $("#update-form input[name='code']");
                            let $id = $("#update-form input[name='id']");
                            let $oneId = $("#update-form input[name='oneId']");
                            $name.val(data.name);
                            $sort.val(data.sort);
                            $oneId.val("");
                            if (data.id == data.code) {
                                $code.val(data.code);
                                $id.val("");
                                $("#sort_menu").css("display", "none");
                                $oneId.val(data.OneId);
                            } else {
                                $id.val(data.id);
                                $("#sort_menu").css("display", "block");
                                $code.val("");
                            }

                            index0pen = layer.open({
                                type: 1,
                                title: "修改",
                                area: ["350px", "250px"],
                                content: $("#update-node"),
                            });
                        } else if (type === "del") {
                            //删除节点
                            // 一级目录无法删除
                            let isWhet = "目录";
                            let msg = "是否删除此" + isWhet + "?";

                            if (data.id == data.code) {
                                index0pen = layer.confirm(
                                    msg,
                                    { icon: 2, title: "提示" },
                                    function (index) {
                                        $.ajax({
                                            url: "/fileType/" + data.OneId,
                                            type: "DELETE",
                                            success: function (rep) {
                                                // console.log(rep);
                                                if (rep.status == 200) {
                                                    //关闭
                                                    layer.close(index0pen);
                                                    layer.msg("删除成功！", { icon: 1 });
                                                    setTimeout(function () {
                                                        window.location.reload();
                                                    }, 300);
                                                } else {
                                                    layer.msg(rep.message, { icon: 2 });
                                                }
                                            },
                                            error: function (error) {
                                                layer.msg("错误:" + error);
                                            },
                                        });

                                        layer.close(index0pen);
                                    }
                                );
                            } else {
                                index0pen = layer.confirm(
                                    msg,
                                    { icon: 2, title: "提示" },
                                    function (index) {
                                        $.ajax({
                                            url: "/dire/" + data.id,
                                            type: "DELETE",
                                            success: function (rep) {
                                                // console.log(rep);
                                                if (rep.status == 200) {
                                                    //关闭
                                                    layer.close(index0pen);
                                                    layer.msg("删除成功！", { icon: 1 });
                                                    setTimeout(function () {
                                                        window.location.reload();
                                                    }, 300);
                                                } else {
                                                    layer.msg(rep.message, { icon: 2 });
                                                }
                                            },
                                            error: function (error) {
                                                layer.msg("错误:" + error);
                                            },
                                        });

                                        layer.close(index0pen);
                                    }
                                );
                            }
                        }
                    },
                });
            }
        },
        complete: function () {
            setTimeout(function () {
                $(
                    ".layui-tree>.layui-tree-set:first-child>.layui-tree-entry>.layui-tree-main"
                ).click();
            }, 500);
        },
        error: function (error) {
            console.warn(error.statusText);
        },
    });
});

layui.use(["form", "tree", "jquery"], function () {
    var form = layui.form;
    var $ = layui.jquery;
    var tree = layui.tree;
    //监听提交

    $(document).on("click", "#add-node [lay-filter='addNode']", function () {
        let data = form.val("add-form");
        if (!data.code) {
            data.code = null;
        } else {
            data.direId = null;
        }

        if (!data.name) {
            layer.msg("请输入名称！");
            return false;
        }

        // console.log(JSON.stringify(data));
        $.ajax({
            url: "/dire",
            type: "POST",
            data: data,
            success: function (rep) {
                if (rep.status == 200) {
                    layer.close(index0pen);
                    layer.msg("新增成功！");

                    setTimeout(function () {
                        window.location.reload();
                    }, 300);
                } else {
                    layer.msg(rep.message);
                }
            },
            error: function (error) {
                layer.msg("错误:" + error);
            },
        });
        return false;
    });

    $(document).on(
        "click",
        "#update-node [lay-filter='updateNode']",
        function () {
            let data = form.val("update-form");

            if (!data.code) {
                data.code = null;
            }

            if (!data.name) {
                layer.msg("请输入名称！");
                return false;
            }

            if (data.oneId) {
                data.id = data.oneId;
                delete data.oneId;
                console.log(data);
                $.ajax({
                    url: "/fileType",
                    type: "PUT",
                    data: data,
                    success: function (rep) {
                        if (rep.status == 200) {
                            layer.close(index0pen);
                            layer.msg("编辑成功！");

                            setTimeout(function () {
                                window.location.reload();
                            }, 300);
                        } else {
                            layer.msg(rep.message);
                        }
                    },
                    error: function (error) {
                        layer.msg("错误:" + error);
                    },
                });
            } else {
                delete data.code;
                delete data.oneId;
                $.ajax({
                    url: "/dire",
                    type: "PUT",
                    data: data,
                    success: function (rep) {
                        if (rep.status == 200) {
                            layer.close(index0pen);
                            layer.msg("编辑成功！");

                            // setTimeout(function () {
                            // 	window.location.reload();
                            // }, 300);
                        } else {
                            layer.msg(rep.message);
                        }
                    },
                    error: function (error) {
                        layer.msg("错误:" + error);
                    },
                });
            }

            return false;
        }
    );
});

layui.use(["layer", "jquery", "form"], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    $(document).on("click", "#add-one-menu", function () {
        index0pen = layer.open({
            type: 1,
            title: "新增档案类型",
            shadeClose: true,
            area: ["400px", "500px"],
            content: $("#add-one-node"),
        });
    });

    $(document).on(
        "click",
        "#add-one-form [lay-filter='addOneNode']",
        function () {
            let data = form.val("add-one-form");

            //唯一标识符，不能以数字开头
            console.log(data.code.length && !isNaN(parseInt(data.code[0])));
            if (data.code.length && !isNaN(parseInt(data.code[0]))) {
                layer.msg("唯一标识符，不能为空或以数字开头");
                return false;
            } else if (!data.name) {
                layer.msg("名字，不能为空");
                return false;
            }

            $.ajax({
                url: "/fileType",
                type: "POST",
                data: data,
                success: function (rep) {
                    if (rep.status == 200) {
                        layer.msg("提交成功！");
                        layer.close(index0pen);
                    } else {
                        layer.msg(rep.message);
                        layer.close(index0pen);
                    }
                    return false;
                },
                error: function (error) {
                    layer.msg(error);
                    layer.close(index0pen);
                    return false;
                },
            });

            return false;
        }
    );
});
